<template>
  <div class="app-container">
    <h5>反馈模板</h5>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="您的姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>
      <el-form-item label="您的年龄">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item label="您的性别">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">新增反馈问题</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleCloses">
      <span>创建类别</span>
      <div class="main">
        <el-form ref="form" :model="form" label-width="5%">
          <el-form-item>
            <el-radio-group v-model="radio">
              <el-radio label="单选" @change="singleselection"></el-radio>
              <el-radio label="多选" @change="multipleselection"></el-radio>
              <el-radio label="单文本框" @change="singletextbox"></el-radio>
              <el-radio label="多文本框" @change="multitextbox"></el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="item" v-show="single">
            <el-tag :key="tag" v-for="tag in dynamicTags" closable :disable-transitions="false" @close="handleClose(tag)">标题：{{tag}}</el-tag>
            <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm"></el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新增选项</el-button>
          </div>
          <div class="item" v-show="multiple">
            <el-tag :key="tag" v-for="tag in dynamicTags" closable :disable-transitions="false" @close="handleClose(tag)">标题：{{tag}}</el-tag>
            <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm"></el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新增选项</el-button>
          </div>
          <div class="item" v-show="singletext">
            <el-form-item label="标题">
              <el-input v-model="form.title"></el-input>
            </el-form-item>
          </div>
          <div class="item" v-show="multipletext">
            <el-form-item label="标题">
              <el-input type="textarea" v-model="form.desc"></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      single: true,
      radio: "单选",
      multiple: false,
      singletext: false,
      multipletext: false,
      dynamicTags: [],
      inputVisible: false,
      inputValue: "",
      form: {
        name: "",
        tel: "",
        sex: "",
        age: "",
        desc: "",
        title: ""
      }
    };
  },
  methods: {
    onSubmit() {
      this.dialogVisible = true;
      // console.log("submit!");
    },
    handleCloses(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 单选
    singleselection() {
      this.single = true;
      this.multiple = false;
      this.singletext = false;
      this.multipletext = false;
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    //多选
    multipleselection() {
      this.multiple = true;
      this.single = false;
      this.singletext = false;
      this.multipletext = false;
    },
    // 单文本框
    singletextbox() {
      this.single = false;
      this.multiple = false;
      this.singletext = true;
      this.multipletext = false;
    },
    // 多文本框
    multitextbox() {
      this.single = false;
      this.multiple = false;
      this.singletext = false;
      this.multipletext = true;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
  background: #fff;
  min-height: 500px;
  .el-input {
    width: 240px !important;
  }
  .el-tag {
    display: block;
    margin-bottom: 10px;
  }
}
</style>
